package com.infodms.dms.dao.claim.basicData;

import java.sql.ResultSet;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import com.infodms.dms.bean.AclUserBean;
import com.infodms.dms.bean.AreaProvinceBean;
import com.infodms.dms.bean.PriceAdjustBean;
import com.infodms.dms.common.Constant;
import com.infodms.dms.dao.common.BaseDao;
import com.infodms.dms.po.TtAsWrLabourPricePO;
import com.infodms.dms.po.TtAsWrModelGroupPO;
import com.infodms.dms.po.TtAsWrPriceAdjustPO;
import com.infodms.dms.util.StringUtil;
import com.infoservice.po3.bean.PO;
import com.infoservice.po3.bean.PageResult;

public class ClaimBasicLaborPriceDao extends BaseDao {
	private ClaimBasicLaborPriceDao(){}
	public static ClaimBasicLaborPriceDao getInstance(){
		return new ClaimBasicLaborPriceDao();
	}
	@Override
	protected PO wrapperPO(ResultSet rs, int idx) {
		// TODO Auto-generated method stub
		return null;
	}

	/*
	 * 查询工时大类
	 */
	@SuppressWarnings("unchecked")
	public PageResult<TtAsWrModelGroupPO> getLaborList(String con,int pageSize,int curPage){
		StringBuffer sql = new StringBuffer("\n");
		sql.append("select distinct * from tt_as_wr_model_group p\n");
		sql.append("where p.wrgroup_type=").append(Constant.WR_MODEL_GROUP_TYPE_01).append("\n");
		if(StringUtil.notNull(con))
			sql.append(con);
		sql.append("order by p.wrgroup_code\n");
		return this.pageQuery(TtAsWrModelGroupPO.class, sql.toString(), null, pageSize, curPage);		
	}
	
	/*
	 * 查询表中的所有工时大类
	 * 生成动态列
	 */
	@SuppressWarnings("unchecked")
	public List<TtAsWrLabourPricePO> getExistLaborList(Long companyId){
		StringBuffer sql = new StringBuffer("\n");
		sql.append("select distinct mode_type\n");
		sql.append("from tt_as_wr_labour_price\n");
		if(companyId!=null)
			sql.append("where oem_company_id=").append(companyId).append("\n");
		sql.append("order by mode_type asc\n");
		return this.select(TtAsWrLabourPricePO.class, sql.toString(), null);
	}
	
	/*
	 * 查询工时单价设定主数据
	 */
	@SuppressWarnings("unchecked")
	public PageResult<Map<String,Object>> mainQuery(String sql,int pageSize,int curPage){
		return (PageResult<Map<String, Object>>)this.pageQuery(sql, null, this.getFunName(), pageSize, curPage);
	}
	
	/*
	 * 判断数据是否已经存在数据库中
	 */
	@SuppressWarnings("unchecked")
	public TtAsWrLabourPricePO existLaborPrice(TtAsWrLabourPricePO po){
		List<TtAsWrLabourPricePO> lists = this.select(po);
		if(lists.size()>0) return lists.get(0);
		return null ;
	}
	
	/****************** 微车 ********************/
	/*
	 * 主页面主查询
	 */
	@SuppressWarnings("unchecked")
	public PageResult<Map<String,Object>> wcMainQuery(String con,int pageSize,int curPage){
		StringBuilder sql= new StringBuilder("\n");
		sql.append("select distinct a.wrgroup_code,\n" );
		List<AreaProvinceBean> list = this.getList() ;
		for(int i=0;i<list.size();i++){
			sql.append("  max(decode(area_level,'").append(list.get(i).getAreaLevel());
			sql.append("',basic_labour_price,'')) \"").append(list.get(i).getAreaLevel()).append("\",\n");
		}
		sql.append("                a.wrgroup_name\n" );
		sql.append("  from tt_as_wr_model_group a, tt_as_wr_labour_price b\n" );
		sql.append(" where a.wrgroup_code = b.mode_type\n" );
		sql.append("   and a.wrgroup_type=").append(Constant.WR_MODEL_GROUP_TYPE_01).append("\n");
		if(StringUtil.notNull(con))
			sql.append(con);
		sql.append(" group by a.wrgroup_code, a.wrgroup_name\n");
		return this.pageQuery(sql.toString(),null,this.getFunName(), pageSize, curPage); 
	}

	/*
	 * 查询大区列表
	 */
	@SuppressWarnings("unchecked")
	public List<AreaProvinceBean> getList(){
		String sql = " select distinct area_level,code_desc from tt_as_wr_labour_price,tc_code c where c.code_id=area_level order by area_level asc\n" ;
		return this.select(AreaProvinceBean.class, sql, null) ;
	}
	
	@SuppressWarnings("unchecked")
	public List<PriceAdjustBean> getAdjustPrice(String mode){
        StringBuffer sql=new StringBuffer("");
        sql.append("select p.id,p.adjust_mode,p.type_adjust,p.adjust_price,c.code_desc from TT_AS_WR_PRICE_ADJUST p,tc_code c where p.type_adjust=c.code_id\n");
		if(StringUtil.notNull(mode))
		{
			sql.append("and p.adjust_mode='"+mode+"'");
		}
        return this.select(PriceAdjustBean.class, sql.toString(), null) ;
	}
	
	@SuppressWarnings("unchecked")
	public PriceAdjustBean getAdjustById(String id)
	{
		String sql="select p.id,p.adjust_mode,p.type_adjust,p.adjust_price,c.code_desc from TT_AS_WR_PRICE_ADJUST p,tc_code c where p.type_adjust=c.code_id and p.id='"+id+"'";
		List<PriceAdjustBean> list= this.select(PriceAdjustBean.class, sql.toString(), null) ;
		if(list!=null)
		{
			if(list.size()>0)
			{
				return list.get(0);
			}
			else return null;
		}
		else return null;
	}
	
	@SuppressWarnings("unchecked")
	public void adjustUpdate(String id,String adjustPrice,Long userId)
	{		
		TtAsWrPriceAdjustPO sel=new TtAsWrPriceAdjustPO();
		TtAsWrPriceAdjustPO upd=new TtAsWrPriceAdjustPO();
		sel.setId(Long.parseLong(id));
		upd.setAdjustPrice(Double.parseDouble(adjustPrice));
		upd.setUpdateBy(userId);
		upd.setUpdateDate(new Date());
		this.update(sel, upd);
	}
	
	@SuppressWarnings("unchecked")
	public void updatePrice(TtAsWrLabourPricePO po,Long userId)
	{
		TtAsWrLabourPricePO sel=new TtAsWrLabourPricePO();
		TtAsWrLabourPricePO upd=new TtAsWrLabourPricePO();
		sel.setId(po.getId());
		upd.setLabourPrice(po.getLabourPrice());
		upd.setUpdateBy(userId);
		upd.setUpdateDate(new Date());
		this.update(sel,upd);		
	}
	
	@SuppressWarnings("unchecked")
	public List<TtAsWrLabourPricePO> getLabourPrice()
	{
		List<Object> pms = new LinkedList<Object>();
		StringBuffer sql=new StringBuffer("");	
		sql.append("select lp.id,(nvl(lp.basic_labour_price, 0) +\n" );
		sql.append("       nvl((select lvpa.adjust_price\n" ); 
		sql.append("              from TT_AS_WR_PRICE_ADJUST lvpa, tm_dealer lvd\n" ); 
		sql.append("             where lvd.dealer_id = lp.dealer_id\n" ); 
		sql.append("               and lvpa.type_adjust = lvd.service_level),\n" ); 
		sql.append("            0) + nvl((select clspa.adjust_price\n" );
		sql.append("                        from TT_AS_WR_PRICE_ADJUST clspa, tm_dealer clsd\n" ); 
		sql.append("                       where clsd.dealer_id = lp.dealer_id\n" ); 
	    sql.append("                         and clspa.type_adjust = clsd.dealer_class),\n" ); 
		sql.append("                      0)) labour_price\n" ); 
		sql.append("  from tt_as_wr_labour_price lp\n" ); 		
		return this.select(TtAsWrLabourPricePO.class, sql.toString(),null);		
		
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
